<?

class Mdl_Categories extends MY_Model {

    function Mdl_Categories() {

        parent::MY_Model();

        $this->limit = 25;

    }

    function get($params = NULL) {

        $this->prep_params($params);

        if (isset($params['category_id'])) {

            $this->db->where('category_id', $params['category_id']);

        }

        if (!isset($params['order_by'])) {

            $this->db->order_by('category');

        }

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $offset = ($this->page * $this->limit) - $this->limit;

            $this->db->limit($this->limit, $offset);

        }

        if (isset($this->is_admin)) {

            $this->db->where('inktype_categories.site_id', $this->session->userdata('site_id'));

        }

        else {

            $this->db->where('inktype_categories.site_id', $this->inktype->site_id);

        }

        $query = $this->db->get('inktype_categories');

        if (isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $this->set_page_links();

        }

        return $query->result();

    }

    function save() {

        $db_array = $this->prep_db_array();

        $db_array['site_id'] = $this->session->userdata('site_id');

        $db_array['url_title'] = url_title($this->input->post('category'));

        $this->db->where('site_id', $this->session->userdata('site_id'));

        $query = $this->db->get('inktype_categories');

        if (!$query->num_rows()) {

            $db_array['is_default'] = 1;

        }

        if (!isset($this->uri_assoc['category_id'])) {

            $this->db->insert('inktype_categories', $db_array);

        }

        else {

            $this->db->where('category_id', $this->uri_assoc['category_id']);

            $this->db->update('inktype_categories', $db_array);

        }

        $this->session->set_flashdata('success_save', TRUE);

    }

    function delete() {

        if (isset($this->uri_assoc['category_id'])) {

            $this->db->where('category_id', $this->uri_assoc['category_id']);

            $this->db->delete('inktype_categories');

            $this->session->set_flashdata('success_delete', TRUE);

        }

    }

    function set_default($category_id) {

        $this->db->where('site_id', $this->session->userdata('site_id'));

        $db_array = array(
            'is_default'    =>  0
        );

        $this->db->update('inktype_categories', $db_array);

        $this->db->where('site_id', $this->session->userdata('site_id'));

        $this->db->where('category_id', $category_id);

        $db_array = array(
            'is_default'    =>  1
        );

        $this->db->update('inktype_categories', $db_array);

    }

    function system_check() {

    // is run from the admin area to notify the admin when no categories exist for the current site

        $this->db->where('inktype_categories.site_id', $this->session->userdata('site_id'));

        $query = $this->db->get('inktype_categories');

        if ($query->num_rows()) {

            return TRUE;

        }

        else {

            return FALSE;

        }

    }

    function validate() {

        $this->fields = array(
            'category'  =>  'Category'
        );

        $this->rules = array(
            'category'  =>  'required'
        );

        return $this->_validate();

    }

}

?>